﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SynapseGaming.LightingSystem.Lights;
using SynapseGaming.LightingSystem.Shadows;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class PointLightSunBurn : Component, IHasActor
    {
        public PointLight PointLight;
        public Actor Actor { get; set; }
        public Renderer Renderer;
        private BoundingSphere RadiusSphere;
        public PointLightSunBurn(Renderer renderer, GameScreen screen)
            : base(screen)
        {
            this.Renderer = renderer;
            PointLight = new PointLight();
            Actor = new Actor("Engine\\Light\\Model\\model_light");
            Actor.Parent = PointLight;
            Actor.Scale = new Vector3(0.5f);
            Actor.ComponentParent = this;

            if (Engine.Editor)
            {
                RadiusSphere = new BoundingSphere(Actor.Position, PointLight.Radius);
            }
            Renderer.SubmitLight(PointLight);
            Renderer.SubmitActor(Actor);               
        }
        public PointLightSunBurn()
        {
        }

        public override void Update()
        {
            base.Update();

            Actor.Update();
            if (Engine.Editor)
            {
                float av = Vector3Util.Average(Actor.Scale);
                if (av > 0.5f || av < 0.5f)
                {
                    float dif = av - 0.5f;
                    if (PointLight.Radius + (dif * 10) > 0)
                    {
                        PointLight.Radius += dif * 10;
                    }
                }
                Actor.Scale = new Vector3(0.5f);
                if (RadiusSphere != null)
                {
                    RadiusSphere.Center = Actor.Position;
                    RadiusSphere.Radius = PointLight.Radius;
                }
            }
            
        }

        public override void PressedWhenSelected()
        {
            DrawSphere = !DrawSphere;
        }


        private bool DrawSphere = false;
        public override void Draw()
        {
            base.Draw();

            Actor.Draw();
            PointLight.Position = Actor.Position;
            if (Engine.Editor)
            {
                if (RadiusSphere != null)
                {
                    if (DrawSphere)
                    {
                        Engine.RenderManager.DrawBoundingSphere(RadiusSphere, 24, Color.White, Renderer.Camera);
                    }
                }
            }
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Data.AddData("Light.DiffuseColor", PointLight.DiffuseColor);
            Data.AddData("Light.Enabled", PointLight.Enabled);            
            Data.AddData("Light.Intensity", PointLight.Intensity);            
            Data.AddData("Light.Radius", PointLight.Radius);
            Data.AddData("Light.ShadowQuality", PointLight.ShadowQuality);
            Data.AddData("Light.ShadowPrimaryBias", PointLight.ShadowPrimaryBias);
            Data.AddData("Light.ShadowSecondaryBias", PointLight.ShadowSecondaryBias);
            Data.AddData("Light.ShadowType", (int)PointLight.ShadowType);
            
            Data.AddData("Renderer", this.Renderer.Name);

            Actor.SerializeData(Data);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            PointLight = new PointLight();
            PointLight.DiffuseColor = Data.GetData<Vector3>("Light.DiffuseColor");
            PointLight.Enabled = Data.GetData<bool>("Light.Enabled");
            PointLight.Intensity = Data.GetData<float>("Light.Intensity");
            PointLight.Radius = Data.GetData<float>("Light.Radius");
            PointLight.ShadowQuality = Data.GetData<float>("Light.ShadowQuality");
            PointLight.ShadowPrimaryBias = Data.GetData<float>("Light.ShadowPrimaryBias");
            PointLight.ShadowSecondaryBias = Data.GetData<float>("Light.ShadowSecondaryBias");
            PointLight.ShadowType = (ShadowType)Data.GetData<int>("Light.ShadowType");

            Actor = new Actor("Engine\\Light\\Model\\model_light");
            Actor.Parent = PointLight;
            Actor.ComponentParent = this;
            Actor.DeserializeData(Data);

            string rendererName = Data.GetData<string>("Renderer");
            if (!string.IsNullOrEmpty(rendererName))
            {
                if (EngineSystem.Renderers.ContainsKey(rendererName))
                {
                    this.Renderer = EngineSystem.Renderers[rendererName];
                    Renderer.SubmitLight(PointLight);
                    Renderer.SubmitActor(Actor);  
                }
            }
            else
            {
                if (EngineSystem.Renderers.Count > 0)
                {
                    this.Renderer = EngineSystem.Renderers.Values.First();
                    Renderer.SubmitLight(PointLight);
                    Renderer.SubmitActor(Actor);  
                }
            }
        }

        public override ICloneable Clone()
        {
            PointLightSunBurn point = new PointLightSunBurn(this.Renderer, this.Parent);

            point.Actor.Position = this.PointLight.Position;

            CopyParametersToOtherLight(point.PointLight);
            point.Actor.ComponentParent = point;
            return point;            
        }

        protected void CopyParametersToOtherLight(PointLight light)
        {
            light.DiffuseColor = this.PointLight.DiffuseColor;
            light.Enabled = this.PointLight.Enabled;
            light.FalloffStrength = this.PointLight.FalloffStrength;
            light.FillLight = this.PointLight.FillLight;
            light.Intensity = this.PointLight.Intensity;
            light.LightingType = this.PointLight.LightingType;
            light.Position = this.PointLight.Position;
            light.Radius = this.PointLight.Radius;
            light.ShadowPerSurfaceLOD = this.PointLight.ShadowPerSurfaceLOD;
            light.ShadowPrimaryBias = this.PointLight.ShadowPrimaryBias;
            light.ShadowQuality = this.PointLight.ShadowQuality;
            light.ShadowSecondaryBias = this.PointLight.ShadowSecondaryBias;
            light.ShadowSource = this.PointLight.ShadowSource;
            light.ShadowType = this.PointLight.ShadowType;
            light.UpdateType = this.PointLight.UpdateType;
        }
    }
}
